package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureMethodDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskMethodDAO;
import cn.edu.scau.cmi.hombio.dao.CureTypeDAO;

import cn.edu.scau.cmi.hombio.domain.CureMethod;
import cn.edu.scau.cmi.hombio.domain.CureTaskMethod;
import cn.edu.scau.cmi.hombio.domain.CureType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CureType entities
 * 
 */

@Service("CureTypeService")

@Transactional
public class CureTypeServiceImpl implements CureTypeService {

	/**
	 * DAO injected by Spring that manages CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodDAO cureMethodDAO;

	/**
	 * DAO injected by Spring that manages CureTaskMethod entities
	 * 
	 */
	@Autowired
	private CureTaskMethodDAO cureTaskMethodDAO;

	/**
	 * DAO injected by Spring that manages CureType entities
	 * 
	 */
	@Autowired
	private CureTypeDAO cureTypeDAO;

	/**
	 * Instantiates a new CureTypeServiceImpl.
	 *
	 */
	public CureTypeServiceImpl() {
	}

	/**
	 */
	@Transactional
	public CureType findCureTypeByPrimaryKey(Integer id) {
		return cureTypeDAO.findCureTypeByPrimaryKey(id);
	}

	/**
	 * Delete an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public CureType deleteCureTypeCureTaskMethods(Integer curetype_id, Integer related_curetaskmethods_id) {
		CureTaskMethod related_curetaskmethods = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(related_curetaskmethods_id, -1, -1);

		CureType curetype = cureTypeDAO.findCureTypeByPrimaryKey(curetype_id, -1, -1);

		related_curetaskmethods.setCureType(null);
		curetype.getCureTaskMethods().remove(related_curetaskmethods);

		cureTaskMethodDAO.remove(related_curetaskmethods);
		cureTaskMethodDAO.flush();

		return curetype;
	}

	/**
	 * Save an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public CureType saveCureTypeCureTaskMethods(Integer id, CureTaskMethod related_curetaskmethods) {
		CureType curetype = cureTypeDAO.findCureTypeByPrimaryKey(id, -1, -1);
		CureTaskMethod existingcureTaskMethods = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(related_curetaskmethods.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskMethods != null) {
			existingcureTaskMethods.setId(related_curetaskmethods.getId());
			existingcureTaskMethods.setMemo(related_curetaskmethods.getMemo());
			related_curetaskmethods = existingcureTaskMethods;
		}

		related_curetaskmethods.setCureType(curetype);
		curetype.getCureTaskMethods().add(related_curetaskmethods);
		related_curetaskmethods = cureTaskMethodDAO.store(related_curetaskmethods);
		cureTaskMethodDAO.flush();

		curetype = cureTypeDAO.store(curetype);
		cureTypeDAO.flush();

		return curetype;
	}

	/**
	 * Delete an existing CureType entity
	 * 
	 */
	@Transactional
	public void deleteCureType(CureType curetype) {
		cureTypeDAO.remove(curetype);
		cureTypeDAO.flush();
	}

	/**
	 * Return a count of all CureType entity
	 * 
	 */
	@Transactional
	public Integer countCureTypes() {
		return ((Long) cureTypeDAO.createQuerySingleResult("select count(o) from CureType o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing CureType entity
	 * 
	 */
	@Transactional
	public Set<CureType> loadCureTypes() {
		return cureTypeDAO.findAllCureTypes();
	}

	/**
	 * Delete an existing CureMethod entity
	 * 
	 */
	@Transactional
	public CureType deleteCureTypeCureMethods(Integer curetype_id, Integer related_curemethods_id) {
		CureMethod related_curemethods = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethods_id, -1, -1);

		CureType curetype = cureTypeDAO.findCureTypeByPrimaryKey(curetype_id, -1, -1);

		related_curemethods.setCureType(null);
		curetype.getCureMethods().remove(related_curemethods);

		cureMethodDAO.remove(related_curemethods);
		cureMethodDAO.flush();

		return curetype;
	}

	/**
	 * Save an existing CureMethod entity
	 * 
	 */
	@Transactional
	public CureType saveCureTypeCureMethods(Integer id, CureMethod related_curemethods) {
		CureType curetype = cureTypeDAO.findCureTypeByPrimaryKey(id, -1, -1);
		CureMethod existingcureMethods = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethods.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureMethods != null) {
			existingcureMethods.setId(related_curemethods.getId());
			existingcureMethods.setMemo(related_curemethods.getMemo());
			related_curemethods = existingcureMethods;
		}

		related_curemethods.setCureType(curetype);
		curetype.getCureMethods().add(related_curemethods);
		related_curemethods = cureMethodDAO.store(related_curemethods);
		cureMethodDAO.flush();

		curetype = cureTypeDAO.store(curetype);
		cureTypeDAO.flush();

		return curetype;
	}

	/**
	 * Return all CureType entity
	 * 
	 */
	@Transactional
	public List<CureType> findAllCureTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CureType>(cureTypeDAO.findAllCureTypes(startResult, maxRows));
	}

	/**
	 * Save an existing CureType entity
	 * 
	 */
	@Transactional
	public void saveCureType(CureType curetype) {
		CureType existingCureType = cureTypeDAO.findCureTypeByPrimaryKey(curetype.getId());

		if (existingCureType != null) {
			if (existingCureType != curetype) {
				existingCureType.setId(curetype.getId());
				existingCureType.setName(curetype.getName());
				existingCureType.setMemo(curetype.getMemo());
			}
			curetype = cureTypeDAO.store(existingCureType);
		} else {
			curetype = cureTypeDAO.store(curetype);
		}
		cureTypeDAO.flush();
	}
}
